package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.EndUserDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 全局顺延状态 0否 1是 Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-09-23
 */
public interface EndUserMapper extends BaseMapper<EndUserDO> {

    String QUERY_SQL_ID = "SELECT tmu.EndUser_ID FROM TMS_MEndUser tmu ";

    String QUERY_SQL = "SELECT tmu.EndUser_ID,tmu.EndUser_OwnerCompany as enduserOwnercompany,tmu.EndUser_companyid,tmu.EndUser_customerid,tmu.EndUser_customercompanyid,tmu.EndUser_clientcode,tmu.EndUser_name,tmu.EndUser_industry," +
            "tmu.EndUser_web,tmu.EndUser_shortname,tmu.EndUser_enname,tmu.EndUser_shortenname,SUBSTRING(tmu.EndUser_Master,0,50) as enduserMaster,tmu.EndUser_license,SUBSTRING(tmu.EndUser_contact,0,50) as enduserContact,SUBSTRING(tmu.EndUser_phone,0,50) as enduserPhone,tmu.EndUser_fax," +
            "tmu.EndUser_mail,tmu.EndUser_address,tmu.EndUser_zip,tmu.EndUser_weixin,tmu.EndUser_logo,tmu.EndUser_bank,tmu.EndUser_bankaccount,tmu.EndUser_description,tmu.EndUser_creator," +
            "tmu.EndUser_inserttime,tmu.EndUser_updater,tmu.EndUser_updatetime,tmu.EndUser_invalid,tmu.EndUser_comments,tmu.EndUser_fromoperator,tmu.EndUser_terminalendid,tmu.EndUser_terminalendcode," +
            "tmu.EndUser_version,tmu.EndUser_postponestatus,tmu.EndUser_situponestatus,tmu.EndUser_setstatus,tmu.EndUser_sendcertificatestatus,tmu.opt_status,tmu.EndUser_guid" +
            ",ISNULL(d.Customer_Name,'') as customerName ,CASE when d.Customer_OwnerCompany=d.Customer_CompanyID THEN 2 ELSE d.Customer_UnderLine end as customerUnderLine " +
            "FROM TMS_MEndUser tmu ";

    String LEFT_JOIN = "LEFT JOIN TMS_MCustomer d on tmu.EndUser_CustomerID = d.Customer_ID " +
            "LEFT JOIN TMS_Company c ON tmu.EndUser_CustomerCompanyID = c.Company_ID ";

    String WHERE = "WHERE tmu.EndUser_OwnerCompany > 0 AND c.Company_Status > 0";

    String ORDER_BY = " ORDER BY tmu.EndUser_ID";

    /**
     * 获取收货方id集合
     *
     * @return
     */
    @Select(QUERY_SQL_ID + LEFT_JOIN + WHERE + ORDER_BY + " OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    List<Long> getConsigneeIds(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select(QUERY_SQL_ID + LEFT_JOIN + WHERE + " and tmu.EndUser_OwnerCompany = #{tenantId}" + ORDER_BY + " OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    List<Long> getConsigneeIdsByTenantId(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("tenantId") Long tenantId);

    @Select("SELECT count(*) FROM TMS_MEndUser tmu " + LEFT_JOIN + WHERE)
    Integer count();

    @Select("SELECT count(*) FROM TMS_MEndUser tmu " + LEFT_JOIN + WHERE + " and tmu.EndUser_OwnerCompany = #{tenantId}")
    Integer countByTenantId(@Param("tenantId") Long tenantId);

    /**
     * 收货方
     *
     * @return
     */
    @Select(QUERY_SQL + LEFT_JOIN + WHERE + ORDER_BY)
    @DataScope(column = "EndUser_OwnerCompany", alias = "tmu")
    List<EndUserDO> list();

    /**
     * 根据id获取收货方
     *
     * @param consigneeId
     * @return
     */
    @Select(QUERY_SQL + LEFT_JOIN + " WHERE tmu.EndUser_ID = #{consigneeId}")
    EndUserDO getById(@Param("consigneeId") Long consigneeId);


    @Select(QUERY_SQL_ID + " WHERE tmu.EndUser_inserttime >= #{startTime} and tmu.EndUser_inserttime <= #{endTime}")
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select(QUERY_SQL_ID + " WHERE tmu.EndUser_OwnerCompany = #{tenantId} and tmu.EndUser_inserttime >= #{startTime} and tmu.EndUser_inserttime <= #{endTime}")
    List<Long> getByTenantIdTime(@Param("tenantId") Long tenantId, @Param("startTime") String startTime, @Param("endTime") String endTime);

}
